<?php
namespace app\admin\controller\nursinguser;;

use app\admin\model\DiseaseUser;
use app\admin\model\TermEmergency;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use app\admin\model\publicuse\PublicUse;
use smartybuilder\libs\Quickbuilder;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="添加关联")
 * Class Guanlian
 */
class Guanlian extends AdminController
{
    use \app\admin\traits\Curd;
     protected $allowModifyFields = [
        'status',
    ];
    protected $noExportFields = [
        'id',
        'name',
        'sort',
        'eat',
        'goto_toilet',
        'operation',
        'the_conduit',
        'disease',
        'sleeps',
        'create_time',
        'update_time',
        'status',
        'delete_time',
    ];
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\admin\model\TermEmergency();
        $array = PublicUse::getConfigDir(__CLASS__);
        if(is_file(__DIR__.DIRECTORY_SEPARATOR.'field'.DIRECTORY_SEPARATOR.$array['class'].'.txt')){
           $this->AllData = json_decode(file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'field'.DIRECTORY_SEPARATOR.$array['class'].'.txt'),true);
        }
    }
    /**
     * @NodeAnotation(title="添加病症")
     */
    public function tianjia(){
        $id = $this->request->param('id');
        $shuju = \app\admin\model\NursingLang::find($id);
        $data = $this->request->param();
        if(isset($data['nursing_lang_id'])&&$data['nursing_lang_id']){
            $nursing_lang_id = $data['nursing_lang_id'];
            $newdata = [];
            $newdata_id = [];
            foreach ($data as $key=>$value){
                if(strpos($key,'key')!==false){
                    $key = str_replace('key','',$key);
                    foreach ($value as $v){
                        $has = $this->model->where('nursing_lang_id',$nursing_lang_id)
                            ->where('option_type_id',$key)
                            ->where('disease_user_id',$v)
                            ->find();
                        if(empty($has)){
                            $newdata[] = [
                                'nursing_lang_id'=>$nursing_lang_id,
                                'option_type_id'=>$key,
                                'disease_user_id'=>$v,
                                'status'=>1,
                                'create_time'=>time(),
                                'update_time'=>time(),
                            ];
                        }else{
                            $newdata_id[] = $has->id;
                        }
                    }
                }
            }
            $bo = Db::name('term_emergency')->where('nursing_lang_id',$nursing_lang_id)
                ->whereNotIn('id',$newdata_id)
                ->delete();
//            echo '<pre>';
//            print_r($newdata);
//            exit;
            $bo = $this->model->saveAll($newdata);
            if(!$bo){
                return $this->error('修改失败');
            }
            return $this->success('修改成功');
        }else{
            if($shuju->getData('type_id')==3){
                $option_type = \app\admin\model\OptionType::where('nursing_lang_id',8)
                    ->select()->toArray();
                $fetch = Quickbuilder::makeForm()
                    ->setAction()
                    ->set_up_form('none','','nursing_lang_id',$id);
                $youde = TermEmergency::where('nursing_lang_id',$id)
                    ->where('status',1)
                    ->order('sort desc')
                    ->column('disease_user_id');
                if(empty($youde)){
                    $youde = [];
                }else{
                    $youde = implode(',',$youde);
                }
                foreach ($option_type as $value){
                    $child = DiseaseUser::where('option_type_id',$value['id'])
                        ->select()->toArray();
                    $fetch = $fetch->set_up_form('lcheckbox',$value['name'],'key'.$value['id'],$youde,$child,'',1,['value'=>'id','show'=>'name']);
                }
                $fetch = $fetch->set_button('submit','提交')
                    ->fetch();
                return $fetch;
            }else{
                $option_type = \app\admin\model\OptionType::where('nursing_lang_id',$id)
                    ->where('status',1)
                    ->order('sort desc')
                    ->select()->toArray();
                $fetch = Quickbuilder::makeForm()
                    ->setAction()
                    ->set_up_form('none','','nursing_lang_id',$id);
                $youde = TermEmergency::where('nursing_lang_id',$id)
                    ->where('status',1)
                    ->order('sort desc')
                    ->column('disease_user_id');
                if(empty($youde)){
                    $youde = '';
                }else{
                    $youde = implode(',',$youde);
                }
                foreach ($option_type as $value){
                    $child = DiseaseUser::where('option_type_id',$value['id'])
                        ->select()->toArray();
                    $fetch = $fetch->set_up_form('lcheckbox',$value['name'],'key'.$value['id'],$youde,$child,'',1,['value'=>'id','show'=>'name']);
                }
                $fetch = $fetch->set_button('submit','提交')
                    ->fetch();
                return $fetch;
            }
        }
    }
    public function add()
    {
        $data = request()->param();
        if ($this->request->isAjax()) {
            $post = $this->request->post();
//            return $this->error('不从',$this->AllData);
            $rule = [];
            $this->validate($post, $rule);
//            return $this->error('保存失败2',$post);
            if(isset($this->AllData['field'])&&!empty($this->AllData['field'])){
                $post = PublicUse::Conversion($post,$this->AllData['field']);
            }
//            dump($post);die();
//            return $this->error('保存失败1',$post);
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        if(isset($this->AllData['field'])){
            if($this->AllData['field']){
                $this->AllData['field'] = PublicUse::FieldGetLianJie($this->AllData['field']);
            }
            if($this->AllData['tabletype']=='tree'){
                if(isset($this->AllData['field']['pid']['option'])&&$this->AllData['field']['pid']['option']){
                    $this->AllData['field']['pid']['option'] = PublicUse::getPidMenuList($this->AllData['field']['pid']['option']);
                }
            }
            $this->AllData['field']['eat']['option'] = DiseaseUser::where('type',1)
                ->where('status',1)
                ->select()->toArray();
            $this->AllData['field']['goto_toilet']['option'] = DiseaseUser::where('type',2)
                ->where('status',1)
                ->select()->toArray();
            $this->AllData['field']['operation']['option'] = DiseaseUser::where('type',3)
                ->where('status',1)
                ->select()->toArray();
            $this->AllData['field']['the_conduit']['option'] = DiseaseUser::where('type',4)
                ->where('status',1)
                ->select()->toArray();
            $this->AllData['field']['disease']['option'] = DiseaseUser::where('type',5)
                ->where('status',1)
                ->select()->toArray();
            $this->AllData['field']['sleeps']['option'] = DiseaseUser::where('type',6)
                ->where('status',1)
                ->select()->toArray();
            $this->assign('alldata',$this->AllData);
        }
        if(isset($data['field'])&&isset($data['id'])&&$data['field']&&$data['id']){
            $data[$data['field']] = $data['id'];
            unset($data['field']);
            unset($data['id']);
        }
        $this->assign('row',$data);
        return $this->fetch();
    }
    public function edit()
    {
        $id = request()->param($this->model->getPk());
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
//            return $this->error('保存失败',$post);
            if(isset($this->AllData['field'])&&!empty($this->AllData['field'])){
                $post = PublicUse::Conversion($post,$this->AllData['field']);
            }
//            return $this->error('保存失败',$post);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        if(isset($this->AllData['field'])){
            if($this->AllData['field']){
                $this->AllData['field'] = PublicUse::FieldGetLianJie($this->AllData['field'],$row);
            }
            if($this->AllData['tabletype']=='tree'){
                if(isset($this->AllData['field']['pid']['option'])&&$this->AllData['field']['pid']['option']){
                    $this->AllData['field']['pid']['option'] = PublicUse::getPidMenuList($this->AllData['field']['pid']['option']);
                }
            }
//            echo '<pre>';
//            print_r($this->AllData['field']['my_lselect']);
//            exit;
            $this->AllData['field']['eat']['option'] = DiseaseUser::where('type',1)
                ->where('status',1)
                ->select()->toArray();
            $this->AllData['field']['goto_toilet']['option'] = DiseaseUser::where('type',2)
                ->where('status',1)
                ->select()->toArray();
            $this->AllData['field']['operation']['option'] = DiseaseUser::where('type',3)
                ->where('status',1)
                ->select()->toArray();
            $this->AllData['field']['the_conduit']['option'] = DiseaseUser::where('type',4)
                ->where('status',1)
                ->select()->toArray();
            $this->AllData['field']['disease']['option'] = DiseaseUser::where('type',5)
                ->where('status',1)
                ->select()->toArray();
            $this->AllData['field']['sleeps']['option'] = DiseaseUser::where('type',6)
                ->where('status',1)
                ->select()->toArray();
            $this->assign('alldata',$this->AllData);
        }
        return $this->fetch();
    }
}